<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\adapay\controller;

class AdminMember extends \app\AdminRest
{
    protected $model;
    protected $rights_model;
    protected $bank_model;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\adapay\model\Member();
        $this->bank_model = new \app\adapay\model\Bank();
    }
    public function configInfo()
    {
        $config_model = new \app\adapay\model\Config();
        $dis = ["uniacid" => $this->_uniacid];
        $data = $config_model->dataInfo($dis);
        if($this->_user["is_admin"] == 0) {
            return $this->success(["status" => $data["status"]]);
        }
        $data["commission"] = $config_model->where($dis)->value("commission");
        return $this->success($data);
    }
    public function configUpdate()
    {
        $input = $this->_input;
        $config_model = new \app\adapay\model\Config();
        $dis = ["uniacid" => $this->_uniacid];
        $data = $config_model->dataUpdate($dis, $input);
        return $this->success($data);
    }
    public function memberList()
    {
        $input = $this->_param;
        $dis[] = ["a.status", ">", -1];
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $where = [];
        if(!empty($input["name"])) {
            $where[] = ["b.card_id", "like", "%" . $input["name"] . "%"];
            $where[] = ["b.card_name", "like", "%" . $input["name"] . "%"];
        }
        $data = $this->model->adminList($dis, $input["limit"], $where);
        if(!empty($data["data"])) {
            $user_model = new \app\massage\model\User();
            foreach ($data["data"] as $k => &$v) {
                $v["user_name"] = $user_model->where(["id" => $v["user_id"]])->value("nickName");
            }
        }
        return $this->success($data);
    }
    public function memberAdd()
    {
        $adapay_model = new \longbingcore\wxcore\Adapay($this->_uniacid);
        $input = $this->_input;
        if(empty($input["member_info"]["user_id"]) && $this->_user["is_admin"] == 0) {
            $input["member_info"]["user_id"] = $this->_user["user_id"];
        }
        $find = $this->model->where(["user_id" => $input["member_info"]["user_id"]])->where("status", ">", -1)->find();
        if(!empty($find)) {
            $this->errorMsg("该用户已有账户");
        }
        $member_id = orderCode();
        $member_info = $input["member_info"];
        $bank_info = $input["bank_info"];
        $member_info["member_id"] = $member_id;
        $member_info["uniacid"] = $this->_uniacid;
        $member_info["order_no"] = orderCode();
        \think\facade\Db::startTrans();
        if($member_info["is_company"] == 1) {
            $res = $adapay_model->createCompanyObj($member_info, 1, 0, $bank_info);
        } else {
            $res = $adapay_model->createUserObj($member_id);
        }
        if($res["status"] == "failed") {
            \think\facade\Db::rollback();
            $this->errorMsg($res["error_msg"]);
        }
        if($res["status"] == "pending") {
            $member_info["status"] = 0;
        }
        $this->model->dataAdd($member_info);
        $order_member_id = $this->model->getLastInsID();
        $bank_info["uniacid"] = $this->_uniacid;
        $bank_info["order_member_id"] = $order_member_id;
        $bank_info["member_id"] = $member_id;
        if($member_info["is_company"] != 1) {
            $res = $adapay_model->createAccountsObj($bank_info);
            if($res["status"] == "failed") {
                \think\facade\Db::rollback();
                $this->errorMsg($res["error_msg"]);
            }
            $bank_info["settle_account_id"] = $res["id"];
        }
        $this->bank_model->dataAdd($bank_info);
        \think\facade\Db::commit();
        return $this->success(true, 200, $order_member_id);
    }
    public function memberUpdate()
    {
        $adapay_model = new \longbingcore\wxcore\Adapay($this->_uniacid);
        $input = $this->_input;
        $member_info = $input["member_info"];
        $bank_info = $input["bank_info"];
        $bank = $this->bank_model->dataInfo(["id" => $input["bank_info"]["id"]]);
        $have = $this->model->where(["user_id" => $member_info["user_id"]])->where("status", ">", -1)->where("id", "<>", $member_info["id"])->find();
        if(!empty($have)) {
            $this->errorMsg("该用户已有账户");
        }
        \think\facade\Db::startTrans();
        $member_info["order_no"] = orderCode();
        if($member_info["is_company"] == 1) {
            $res = $adapay_model->createCompanyObj($member_info, 2, 0, $bank_info);
            if($res["status"] == "failed") {
                \think\facade\Db::rollback();
                $this->errorMsg($res["error_msg"]);
            }
            if($res["status"] == "pending") {
                $member_info["status"] = 0;
            }
        }
        $this->model->dataUpdate(["id" => $member_info["id"]], $member_info);
        $bank_info["member_id"] = $member_info["member_id"];
        $res = $adapay_model->delAccountsObj($bank);
        $res = $adapay_model->createAccountsObj($bank_info);
        if($res["status"] == "failed") {
            \think\facade\Db::rollback();
            $this->errorMsg($res["error_msg"]);
        }
        $bank_info["settle_account_id"] = $res["id"];
        $this->bank_model->dataUpdate(["id" => $bank_info["id"]], $bank_info);
        \think\facade\Db::commit();
        return $this->success(true);
    }
    public function memberStatusUpdate()
    {
        $input = $this->_input;
        $this->model->dataUpdate(["id" => $input["id"]], ["status" => $input["status"]]);
        return $this->success(true);
    }
    public function memberInfo()
    {
        $input = $this->_param;
        $data["member_info"] = $this->model->dataInfo(["id" => $input["id"]]);
        $data["bank_info"] = $this->bank_model->dataInfo(["order_member_id" => $data["member_info"]["id"], "status" => 1]);
        $user_model = new \app\massage\model\User();
        $data["member_info"]["nickName"] = $user_model->where(["id" => $data["member_info"]["user_id"]])->value("nickName");
        if(empty($data["bank_info"]["cert_id"])) {
            $data["bank_info"]["cert_id"] = $data["member_info"]["legal_cert_id"];
        }
        return $this->success($data);
    }
    public function adminPay()
    {
        $input = $this->_input;
        $model = new \app\adapay\model\AdminPay();
        $res = $model->adminPay($this->_uniacid, $input["pay_price"]);
        if($res["status"] == "succeeded") {
            $code = getCode($this->_uniacid, $res["expend"]["qrcode_url"]);
            $expiration_time = $res["created_time"] + 7200;
            $data["code"] = $code;
            $data["expiration_time"] = $expiration_time;
            $data["order_code"] = $res["id"];
            return $this->success($data);
        }
        return $this->error($res["error_msg"]);
    }
    public function checkAdminPay()
    {
        $input = $this->_param;
        $order_model = new \app\adapay\model\AdminPay();
        $dis = ["adapay_id" => $input["order_code"], "pay_type" => 2];
        $find = $order_model->dataInfo($dis);
        $res = !empty($find) ? 1 : 0;
        return $this->success($res);
    }
    public function adminInfo()
    {
        $pay_model = new \app\adapay\model\AdminPay();
        $cash = $pay_model->where(["pay_type" => 2])->sum("true_price");
        $canuse_cash = $pay_model->whereTime("pay_time", "-547 days")->where(["pay_type" => 2])->sum("true_price");
        $data["cash"] = round($canuse_cash, 2);
        $data["over_cash"] = round($cash - $canuse_cash, 2);
        $record_model = new \app\adapay\model\AccountsRecord();
        $data["accounts_cash"] = $record_model->where(["uniacid" => $this->_uniacid])->sum("cash");
        return $this->success($data);
    }
    public function adminAdapay()
    {
        $admin_model = new \app\massage\model\Admin();
        $user_id = $admin_model->where(["id" => $this->_user["id"]])->value("user_id");
        $data["member_info"] = $this->model->where(["user_id" => $user_id])->where("status", ">", -1)->find();
        if(empty($data["member_info"])) {
            return $this->success([]);
        }
        if(!empty($data["member_info"])) {
            $data["bank_info"] = $this->bank_model->dataInfo(["order_member_id" => $data["member_info"]["id"], "status" => 1]);
            $user_model = new \app\massage\model\User();
            $data["member_info"]["nickName"] = $user_model->where(["id" => $data["member_info"]["user_id"]])->value("nickName");
        }
        return $this->success($data);
    }
}

